package com.cuitrip.business.conversation;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.inputmethod.InputMethodManager;

import com.cuitrip.business.order.detail.OrderSubFragment;
import com.cuitrip.business.order.model.OrderItem;
import com.lab.logtrack.AppLog;
import com.lab.utils.LogHelper;

import io.rong.imkit.fragment.ConversationFragment;
import io.rong.imlib.model.Conversation;

/**
 * Created by baziii on 15/9/2.
 */
public class CtConversationFragment extends ConversationFragment implements OrderSubFragment{
    public static final String ORDER_ID = "CtConversationFragment.ORDER_ID";
    //传这个参数真是太蛋疼叻
    public static final String RECEIVE_ID = "CtConversationFragment.RECEIVE_ID";
    public static final String OLD_VERSION_MSG_FLAG = "CtConversationFragment.OLD_VERSION_MSG_FLAG";

    InputMethodManager manager;

    public static CtConversationFragment newInstance(String orderId, boolean hasOldVersionMessage,
                                                     String otherId) {

        Bundle args = new Bundle();
        args.putString(ORDER_ID, orderId);
        args.putString(RECEIVE_ID, otherId);
        args.putBoolean(OLD_VERSION_MSG_FLAG, hasOldVersionMessage);
        CtConversationFragment fragment = new CtConversationFragment();
        fragment.setArguments(args);
        LogHelper.e("omg", "newInstance  " + hasOldVersionMessage);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    public void setUri(ApplicationInfo applicationInfo, OrderItem orderItem){
        String target = orderItem.getTargetId();
        AppLog.d("build fragment   target id" + target);
        String title = orderItem.getServiceName();
        Uri uri = Uri.parse("rong://" + applicationInfo.packageName).buildUpon().appendPath("conversation")
                .appendPath(Conversation.ConversationType.DISCUSSION.getName().toLowerCase())
                .appendQueryParameter("targetId", target).appendQueryParameter("title", title).build();
        setUri(uri);
    }


    @Override
    public void onLeaveMe(Activity activity) {

        if (activity != null && activity.getCurrentFocus() != null){
            manager  = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            try{
                manager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }catch (Exception e){
                //防止出键盘错误
            }
        }

    }
}
